package com.youmingxing.adpter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.activity.selfInfo.PersonalInfoActivity;
import com.youmingxing.data.MyNews;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by chihiro on 15/9/2.
 */
public class MyNewsAdapter extends BaseAdapter {
    private Context context;
    private List<MyNews> mList = new ArrayList<>();
    private LayoutInflater inflater;

    public MyNewsAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setData(List<MyNews> list) {
        if (list == null)
            return;
        if (mList == null) {
            mList = list;
        } else {
            mList.clear();
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    private class ViewHolder {
        TextView name;
        ImageView headImg;
        TextView alert;
        TextView content;
        ImageView likeImg;
        ImageView thumbnail;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_my_news, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.headImg = (ImageView) convertView.findViewById(R.id.img_avatar);
            holder.likeImg = (ImageView) convertView.findViewById(R.id.img_readHeart);
            holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
            holder.alert = (TextView) convertView.findViewById(R.id.tv_news_content);
            holder.content = (TextView) convertView.findViewById(R.id.tv_comments);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final MyNews myNews = mList.get(position);
        //username
        holder.name.setText(myNews.getNickname());
        //头像
        Glide.with(context).load(myNews.getHeadimg())
                .placeholder(R.drawable.default_avatar).error(R.drawable.default_avatar)
                .bitmapTransform(new CropCircleTransformation(
                        context)).crossFade().into
                (holder.headImg);
        holder.content.setText(myNews.getContent());
        holder.alert.setText(myNews.getNickname() + myNews.getAlert());

        if (myNews.getType() == 1) {// 1点赞  2 评论  3 投票
            holder.likeImg.setVisibility(View.VISIBLE);
            holder.content.setVisibility(View.INVISIBLE);
        } else if(myNews.getType() == 2){
            holder.content.setVisibility(View.VISIBLE);
            holder.likeImg.setVisibility(View.INVISIBLE);
        } else {
            holder.content.setVisibility(View.VISIBLE);
            holder.likeImg.setVisibility(View.INVISIBLE);
        }
        if(myNews.getThumbnail().equals("")){
            holder.thumbnail.setVisibility(View.INVISIBLE);
        }
        Glide.with(context).load(myNews.getThumbnail())
                .crossFade().into(holder.thumbnail);
        holder.headImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PersonalInfoActivity.class);
                intent.putExtra("u_id", myNews.getU_id());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

}
